<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../2305/js/vue.js"></script>
</head>
<body>
    <!-- div id为app挂载点 -->
    <div id="app">
        <h2>总数:{{total}}</h2>
        <my-btn @increase="getTotal"></my-btn>
    </div>
    <!-- 定义组件模板 -->
     <template id="temp">
        <div>
            <button v-on:click="add">单击+1</button>
            <button @click="red">单击-1</button>
        </div>
     </template>
<!-- 使用javascript脚本 -->
    <script type="text/javascript">
        Vue.component('my-btn',{
            template:'#temp',
            data(){
                return{
                    count:0
                }
            },
            methods:{
                add(){
                    this.count++;
                    console.log('add() 加速'+this.count);
                    this.$emit('increase',this.count);
                },
                red(){
                    this.count--;
                    console.log('add() 减速'+this.count);
                    this.$emit('increase',this.count);
                }
            }
        })
        var vm = new Vue({
            el:'#app',
            data:{
                total:0
            },
            methods: {
                getTotal(t) {
                    this.total = t;
                    console.log('getTotal() 获取到子组件传递过来的值'+t);
                }
            }
        })
    </script>
</body>
</html>